package com.core.utility;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class CreateFile extends ActionSupport implements ServletRequestAware{

	/**
	 * 
	 */
	String files;
	HttpServletRequest request;
	private static final long serialVersionUID = 1L;
	
	public String execute()
	{
		StringBuilder formBuilder = new StringBuilder("long id;<br>");
		
		StringBuilder entityBuilder = new StringBuilder("@Id<br>@GeneratedValue<BR>@Column(name=\"id\")<br>long id;<br>");
		Iterator<Entry<String,String[]>> itr = request.getParameterMap().entrySet().iterator();
		String key;
		String value;
		String prefix;
		String column;
		String vtype;
		StringBuilder entityVariable = new StringBuilder();
		
		while(itr.hasNext())
		{
			Entry<String, String[]> etr = itr.next();
			
			key = etr.getKey();
			value=etr.getValue()[0];
			column = key.replaceAll("\\p{Upper}+", "_$0").toUpperCase();
			try{
				Integer.parseInt(value);
				prefix="NM_";
				vtype="int ";
				
				
			}catch(Exception ex){prefix="VC_";vtype="String ";}
			
			formBuilder.append("String "+key).append(";<BR>");
			entityVariable.append(vtype+" "+key).append(";<BR>");
			//entityBuilder.append("@Column(name=\""+prefix+column+"\")<br>String "+key).append(";<BR>");
			
			String str = "<BR>public "+vtype+" get"+toProperCase(key)+"()<BR>{<BR>return this."+key+";<BR>}";
			entityBuilder.append("<BR>@Column(name=\""+prefix+column+"\")"+str);
			
			System.out.println(etr.getKey()+"-----"+etr.getValue()[0].toString());
		}
		files=  formBuilder.toString()+"<br><br>"+entityVariable.toString()+"<br><br>"+entityBuilder.toString();
		
		Pattern p = Pattern.compile("\\p{Upper}+");
        // Split input with the pattern
		

		return "success";
		}

	
	public String getFiles() {
		return files;
	}

	public void setFiles(String files) {
		this.files = files;
	}


	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request=arg0;
		
	}
	private String toProperCase(String name) {
	    return name.substring(0, 1).toUpperCase() +
	               name.substring(1);
	}



}
